package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.*;
import com.team.six.bluecatshop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    SkuService skuService;
    @Autowired
    CustomerService customerService;
    @Autowired
    AddressService addressService;
    @Autowired
    IndentService indentService;


    /**
     * 完善自己的信息
     */
    @PostMapping("/complete1")
    public String complete1(Customer customer, String customerAddress){
        addressService.addAddress(customer,customerAddress);
        customerService.updateByPrimaryKeySelective(customer);
        return "redirect:/customer/person_center";
    }

    /**
     *跳转至个人信息界面
     */
    @GetMapping("/accountInformation")
    public String accoutInformation(String customerId,Model model){
        Customer customer = customerService.findById(customerId);
        model.addAttribute("customer",customer);
        return "account-information";
    }

    /**
     * 昵称修改
     */
    @PostMapping("/customerNikNameUpdate")
    @ResponseBody
    public Customer customerNikNameUpdate(Customer customer){
        customerService.updateByPrimaryKeySelective(customer);
        return customer;
    }

    /**
     * 邮箱修改
     */
    @PostMapping("/customerEmailUpdate")
    @ResponseBody
    public Customer customerEmailUpdate(Customer customer){
        customerService.updateByPrimaryKeySelective(customer);
        return customer;
    }

    /**
     * 手机号修改
     */
    @PostMapping("/customerPhoneUpdate")
    @ResponseBody
    public Customer customerPhoneUpdate(Customer customer){
        customerService.updateByPrimaryKeySelective(customer);
        return customer;
    }


    /**
     * 密码修改
     */
    @PostMapping("/customerPasswordUpdate")
    @ResponseBody
    public Customer customerPasswordUpdate(Customer customer){
        customerService.updateByPrimaryKeySelective(customer);
        return customer;
    }

    /**
     * 跳转至地址管理界面
     */
    @GetMapping("/addressManage")
    public String addressManage(String customerId,Model model){
        System.out.println(customerId);
        List<Address> address = addressService.findAddressByCustomerId(customerId);
        Customer customer = customerService.findById(customerId);
        model.addAttribute("customer",customer);
        model.addAttribute("address",address);
        return "account-number-center";
    }

    /**
     * 修改地址
     */
    @PostMapping("/addressUpdate")
    @ResponseBody
    public Address addressUpdate(Address address){
        addressService.updateByPrimaryKeySelective(address);
        return address;
    }

    /**
     * 新增地址
     */
    @PostMapping("addAddress")
    @ResponseBody
    public Address addAddress(Address address){
        addressService.insert(address);
        return address;
    }
    /**
     * 删除地址
     */
    @PostMapping("/delete_address")
    @ResponseBody
    public Integer delAddress(String addressId){
        return  addressService.deleteByPrimaryKey(addressId);
    }

    /**
     * 回显完善信息
     */
    @RequestMapping("/person_center")
    public String personCenter(Model model,HttpSession session){
        String  customerId = (String) session.getAttribute("customerId");
        Customer customer=customerService.findById(customerId);
        model.addAttribute("customer",customer);
        //System.out.println(customerId);
        List<Indent> indentsIdentifyZero = indentService.findByCustomerIdAndIdentify0(customerId.toString());
        List<Indent> indentsIdentifyOne = indentService.findByCustomerIdAndIdentify1(customerId.toString());

        List<Address> addresses=addressService.findAddressByCustomerId(customerId);

        if(addresses.size() > 0){
            //System.out.println("进来了");
            model.addAttribute("address",addresses.get(0).getAddress());
        }
        session.setAttribute("indentsIdentifyZero",indentsIdentifyZero);
        session.setAttribute("indentsIdentifyOne",indentsIdentifyOne);
        return "personal-center";
    }
}
